﻿/*
Copyright 2010 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;

namespace Comkarl.Architecture.Patterns.MVC.Entities
{
    /// <summary>
    /// Represents an action constructed at run-time.
    /// </summary>
    /// <typeparam name="TIdentifierName">
    /// The type for identifier's name.
    /// </typeparam>
    /// <typeparam name="TActionResult">
    /// The type for action's result.
    /// </typeparam>
    public sealed class RuntimeAction<TIdentifierName, TActionResult> : Comkarl.Architecture.Patterns.MVC.Entities.Action<TIdentifierName>
        where TIdentifierName : IEquatable<TIdentifierName>
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="handler">
        /// Defines the handler function performing the behaviors for this action.
        /// </param>
        public RuntimeAction(RuntimeActionDelegate handler)
            : base(new ActionIdentifier<TIdentifierName>(default(TIdentifierName)))
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            this.handler = handler;
        }

        private readonly RuntimeActionDelegate handler;

        /// <summary>
        /// Gets the handler function performing the behaviors for this action.
        /// </summary>
        private RuntimeActionDelegate Handler
        {
            get
            {
                return handler;
            }
        }

        /// <summary>
        /// Does the code provided by the handler delegate.
        /// </summary>
        /// <typeparam name="TResult">
        /// The result type for the action.
        /// </typeparam>
        /// <returns>
        /// The action result.
        /// </returns>
        public override object Do()
        {
            return Handler();
        }
    }
}